package productmanagementclient.db.adapter;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import productmanagementclient.db.utils.SessionFactoryUtil;
import productmanagementclient.publish.Product;

public class ProductAdapter {
	
	private static ProductAdapter instance = new ProductAdapter();
	
	public static ProductAdapter getInstance() {
		return instance;
	}
	
	private ProductAdapter() {
		System.setProperty("org.xml.sax.driver","org.apache.crimson.parser.XMLReaderImpl");
	}
	public List<Product> getAllProduct() {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Product> list = null;
		try {
			transaction = session.beginTransaction();
			Query query = session.createQuery("from Product");
			list = query.list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
			e.printStackTrace();
			throw new IllegalArgumentException("Cannot connect to data base");
		} finally {
			session.close();
		}

		return list;
	}
	
	public Product getProductById(Integer productID) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;		
		Product product = null;
		try {
			transaction = session.beginTransaction();
			product = (Product) session.get(Product.class, productID);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return product;
	}
	
	public Product getProductByName(String productName) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;		
		Product product = null;
		try {
			transaction = session.beginTransaction();
			//product = (Product) session.get(Product.class, productID);			
			Query query = session.createQuery("from Product as p where p.name = :name");			
		    query.setString("name", productName);
		    product = (Product) query.uniqueResult();			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return product;
	}

	public Long saveProduct(Product product) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long productId = null;
		try {
			transaction = session.beginTransaction();
			productId = (Long) session.save(product);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return productId;
	}
	
	public void updateProduct(Product newProduct) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Product product = (Product) session.get(Product.class, newProduct.getId());
			product.setName(newProduct.getName());
			product.setExportCost(newProduct.getExportCost());
			product.setImportCost(newProduct.getImportCost());
			product.setShippingCost(newProduct.getShippingCost());
			product.setImportDate(newProduct.getImportDate());
			product.setNote(newProduct.getNote());
			product.setProductType(newProduct.getProductType());
			product.setRemainingItem(newProduct.getRemainingItem());	
			//session.update(product);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void deleteProduct(Long productId) {
		Session session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Product product = (Product) session.get(Product.class, productId);
			if (product != null)
				session.delete(product);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

//	public static void main(String args[]) {
//		ProductAdapter pa = new ProductAdapter();
//		pa.saveProduct(new Product("product 99999", 5000.00, 8000.00, 1000.00, "type bbb", 3, "note"));
//		
////		pa.updateProduct(new Product(6, "product 66", 5000.00, 8000.00, 1000.00, "type bbb", 3, "note"));
//		
////		pa.deleteProduct((long)4);
//		
//		List<Product> allProduct = pa.getAllProduct();
//		for (Product product : allProduct) {
//			System.out.println(product.getName());
//		}
		
//		Product p = pa.getProductByName("product 1");
//		System.out.println(p);
		
//		UserAdapter ua = new UserAdapter();
//		pa.saveUser(new User("user1", "user1","user1@yahoo.com","address", "user"));
//		pa.saveUser(new User("user2", "user2","user2@yahoo.com","address", "user"));
//		pa.saveUser(new User("user3", "user3","user3@yahoo.com","address", "user"));

//		pa.updateUser(new User(6, "user4", "user4","user4@yahoo.com","address", "user"));		
//		pa.deleteUser((long)4);
		
//		List<User> allUser = pa.getAllUser();
//		for (User user : allUser) {
//			System.out.println(user.getUserName());
//		}
		
//		User u = ua.getUserByName("admin");
//		System.out.println(u);
//	}
}
